package cn.zs.middleware.trigger.http;

import cn.zs.middleware.domain.xxx.adapter.repository.IAwardRepository;
import cn.zs.middleware.domain.xxx.model.entity.AwardEntity;
import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CacheUpdate;
import com.alicp.jetcache.anno.Cached;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Auther: zs
 * @Date: 2025/05/13/09:52
 */
@RestController
@RequestMapping("/award")
public class AwardController {

    @Resource
    private IAwardRepository awardRepository;

    @GetMapping("/getRemote")
    @Cached(name="AwardCacheRemote:", key = "#id", expire = 3600, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.REMOTE)
    public AwardEntity getAwardById(int id){
        AwardEntity awardEntity = awardRepository.queryAwardById(id);
        System.out.println("第一次获取数据，未走缓存："+id);

        return awardEntity;
    }

    @GetMapping("/getLocal")
    @Cached(name="AwardCacheLocal:", key = "#id", expire = 3600, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.LOCAL)
    public AwardEntity getLocal(int id){
        AwardEntity awardEntity = awardRepository.queryAwardById(id);
        System.out.println("第一次获取数据，未走缓存："+id);
        return awardEntity;
    }

    @GetMapping("/getBoth")
    @Cached(name="AwardCache:", key = "#id", expire = 3600, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.BOTH)
    public AwardEntity getBoth(int id){
        AwardEntity awardEntity = awardRepository.queryAwardById(id);
        System.out.println("第一次获取数据，未走缓存："+id);
        return awardEntity;
    }

    @PostMapping("/updateAward")
    @CacheUpdate(name="AwardCacheRemote:", key = "#award.id",value="#award")
    public boolean updateAward(@RequestBody AwardEntity award){


        return awardRepository.updateAward(award);
    }

    @PostMapping("deleteAward")
    @CacheInvalidate(name = "AwardCacheRemote:", key = "#id")
    public Boolean deleteUser(Long id){
        // TODO 从数据库删除
        return true;
    }
}
